from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.template import loader
from django.urls import reverse
from myDefensor.models import ArgsName,ArgValueSubmit,xssInput
# Create your views here.

	
def index(request):
	template = loader.get_template('myDefensor/index.html')
	ArgsList = ArgsName.objects.filter(id__in = [22,28,54,56,57,59,60,62,63,64,65,66,67,68,69,70])
	submitData = ArgValueSubmit.objects.all()
	submitData = generateStyle(submitData)
	context = {"ArgsList":ArgsList,"submitData":submitData}
	return HttpResponse(template.render(context,request))
def xssInputPage(request):
	return render(request, 'myDefensor/xssInput.html', None)
def tryOpen(request):
	return render(request, 'myDefensor/tryOpen.html', None)
def argShow(request):
	ArgsList = ArgsName.objects.filter(id__in = [22,28,54,56,57,59,60,62,63,64,65,66,67,68,69,70])
	submitData = ArgValueSubmit.objects.all()
	submitData = generateStyle(submitData)
	hint = ''
	if "hint" in request.session.keys():
		hint =request.session["hint"]
	context = {"ArgsList":ArgsList,"rst":hint,"submitData":submitData}
	return render(request, 'myDefensor/index.html', context)


# def tempTest(request):
# 	对判断结果做处理，应该放在detector中，这里是前台应用，尽量不侵入
# 	cont = '<p>argName = '+request.session["argName"]+"</p> <p>argValue = "+request.session["argValue"]+"</p>"
# 	cont = cont + "<p>remoteIP = "+request.session["ip"]+"</p> <p>argPath="+ request.session["argPath"]+"</p>"
# 	print("console:",cont)
# 	# start handliing args
# 	argValue = request.session["argValue"]
# 	argName = request.session["argName"]
# 	argPath = request.session["argPath"]
# 	remoteIp = request.session["ip"]
# 	judgeRst = request.session["judgeRst"]
# 	judgeType = "lmRst:"+str(judgeRst["lm"])+";cmRst:"+str(judgeRst["cm"])
# 	rawTarget = request.session["rawTarget"]
# 	# save info as example
# 	Arg = ArgValueSubmit(route = argPath,arg_name = argName,source_ip = remoteIp,type_judgement=judgeType,value = argValue,value_len = argValue.__len__())
# 	Arg.save()
# 	# end handling args
# 	del request.session["argValue"]
# 	del request.session["argName"]
# 	del request.session["ip"]
# 	del request.session["argPath"]
# 	del request.session["rawTarget"]
# 	return HttpResponseRedirect(reverse("myDefensor:argShow"))

# tools
def decideStyle(rstString):
	if rstString == "lmRst:1;cmRst:1":
		return ''
	elif rstString == "lmRst:1;cmRst:0" or rstString == "lmRst:0;cmRst:1":
		return 'color: blue;'
	elif rstString == "lmRst:0;cmRst:0":
		return 'color: red;'
def generateStyle(dataList):
	for x in dataList:
		x.style = decideStyle(x.type_judgement)
	return dataList